﻿--[[
@brief      游戏录像提示框
@date       2017-01-08
@author     jameszhai
]]

EGMRecTipsConst = 
{
    nBtnSpace = 10;

    sTipsXML = "GameRecTips.tpf";           -- 界面文件
    sDefIcon = "main\\default_icon.png";    -- 默认的游戏图标
};

EGMRecUI = 
{
    sEdtText   = "edtInfo";
    sImgIcon   = "imgIcon";
    sBtnClose  = "TitleBarCtn\\btnClose";
    sBtnEditV  = "btnEditVid";
    sBtnEditP  = "btnEditImg";
    sBtnCancel = "btnCancel";
};

ERptCmd = 
{
    eShowTips = 0;
    eClkEditV = 1;
    eClkEditP = 2;
    eClkClose = 3;
};

TGMRecTipsView = 
{
    -- 初始化
    InitView = function()
        if nil ~= TGMRecTipsView.pDlgView then
            TGPLog.LogI("GMRecTipsView", "View already inited.");
            return true;
        end;

        local   dwViewID = layout_mgr.restore_layout(EGMRecTipsConst.sTipsXML);
        local   pUILib = ui_utility.uilib();

        if (nil ~= pUILib) and (nil ~= dwViewID) and (dwViewID > 0) then
            TGMRecTipsView.pDlgView  = pUILib:GetControlPtr(dwViewID);
            TGMRecTipsView.pEdtText  = ui_utility.get_ctrl_ptr_by_path(dwViewID, EGMRecUI.sEdtText);

            if (nil == TGMRecTipsView.pEdtText) then
                TGPLog.LogE("GMRecTipsView", "Get Edit text fail.");
                return false;
            end;

            TGMRecTipsView.pBtnEdtV  = ui_utility.get_ctrl_ptr_by_path(dwViewID, EGMRecUI.sBtnEditV);
            if (nil == TGMRecTipsView.pBtnEdtV) then
                TGPLog.LogE("GMRecTipsView", "Get button edit video fail.");
                return false;
            end;
            ui_utility.setproperty_by_id(TGMRecTipsView.pBtnEdtV:GetControlID(), "OnClick", 
                                      "TGMRecTipsView.OnClkEditV");

            TGMRecTipsView.pBtnEdtP  = ui_utility.get_ctrl_ptr_by_path(dwViewID, EGMRecUI.sBtnEditP);
            if (nil == TGMRecTipsView.pBtnEdtP) then
                TGPLog.LogE("GMRecTipsView", "Get button edit image fail.");
                return false;
            end;
            ui_utility.setproperty_by_id(TGMRecTipsView.pBtnEdtP:GetControlID(), "OnClick", 
                                      "TGMRecTipsView.OnClkEditP");

            TGMRecTipsView.dwImgIcon = ui_utility.get_ctrl_id_by_path(dwViewID, EGMRecUI.sImgIcon);

            local   dwClose = ui_utility.get_ctrl_id_by_path(dwViewID, EGMRecUI.sBtnClose);

            if (TGMRecTipsView.IsValidID(dwClose)) then
                ui_utility.setproperty_by_id(dwClose, "OnClick", "TGMRecTipsView.OnClkClose");
            else
                TGPLog.LogE("GMRecTipsView", "Get close button fail.");
                return false;
            end;

            TGMRecTipsView.pBtnCel = ui_utility.get_ctrl_ptr_by_path(dwViewID, EGMRecUI.sBtnCancel);

            if nil ~= TGMRecTipsView.pBtnCel then
                ui_utility.setproperty_by_id(TGMRecTipsView.pBtnCel:GetControlID(), "OnClick", 
                                             "TGMRecTipsView.OnClkClose");
            else
                TGPLog.LogE("GMRecTipsView", "Get cancel button fail.");
                return false;
            end;

            -- 窗口关闭
            ui_utility.setproperty_by_id(dwViewID, "OnDialogClose", "TGMRecTipsView.OnDlgClose")
        else
            TGPLog.LogE("GMRecTipsView", "Create tips dialog fail.");
            return false;
        end;

        return true;
    end;

    -- 卸载
    UnInitView = function()
        TGMRecTipsView.pDlgView = nil;
        TGMRecTipsView.pEdtText = nil;
        TGMRecTipsView.pLblIcon = nil;
        TGMRecTipsView.pBtnEdtV = nil;
        TGMRecTipsView.pBtnEdtP = nil;
    end;

    -- 显示界面
    ShowView = function(zParam)
        if (nil == TGMRecTipsView.pEdtText) or (nil == TGMRecTipsView.pDlgView) then
            TGPLog.LogE("GMRecTipsView", "Text edit is nil.");
            return;
        end;

        TGMRecTipsView.zEdtParam = zParam;

        local   nVideo = zParam.video_num;  -- 视频个数
        local   nImage = zParam.shot_num;   -- 图片个数
        local   sTips  = nil;

        if (nVideo ~= nil and nVideo > 0) then
            sTips = LoadStr("GAMEVIDEO_VIDEOPOSSESS_PF") .. "<f c=\"0x296ee3\">" .. tostring(nVideo) .. "</f>" .. LoadStr("GAMEVIDEO_VIDEOPOSSESS_SF");
        else 
            nVideo = 0;
        end;

        if (nImage ~= nil and nImage > 0) then
            if nil == sTips then
                sTips = LoadStr("GAMEVIDEO_PICTUREPOSSESS_PF") .. "<f c=\"0x296ee3\">" .. tostring(nImage) .. "</f>" .. LoadStr("GAMEVIDEO_PICTUREPOSSESS_SF");
            else
                sTips = sTips .. LoadStr("GAMEVIDEO_AND") .. "<f c=\"0x296ee3\">" .. tostring(nImage) .. "</f>" .. LoadStr("GAMEVIDEO_PICTUREPOSSESS_SF");
            end;
        else
            nImage = 0;
        end;

        if (nil == sTips) then
            TGPLog.LogI("GMRecTipsView", "No video and no snap image.");
            return;
        end;

        if nVideo > 0 then
            sTips = "<f c=\"0x000000\">" .. sTips .. 
                "</f><br><f c=\"0x948388\" size=\"-12\">" .. LoadStr("GAMEVIDEO_NEEDEDIT&UPLOAD") .. "</f>";
        else
            sTips = "<f c=\"0x000000\">" .. sTips .. 
                "</f><br><f c=\"0x948388\" size=\"-12\">" .. LoadStr("GAMEVIDEO_NEEDEDIT").. "</f>";
        end
        

        local   sText = U8toCUIStr(sTips);

        TGMRecTipsView.pEdtText:Clear();
        TGMRecTipsView.pEdtText:PushBackHtmlString(sText);

        -- 图片
        if (0 ~= TGMRecTipsView.dwImgIcon) then
            local   sImage = "ONEIMAGE:";

            if (nil ~= zParam.icon) then
                sImage = sImage .. EGMRecTipsConst.sDefIcon .. "|" .. zParam.icon;
            else
                sImage = sImage .. EGMRecTipsConst.sDefIcon;
            end;

            sImage = sImage .. ";Center;0";

            ui_utility.setproperty_by_id(TGMRecTipsView.dwImgIcon, "Background", 
                                        sImage);
        else
            TGPLog.LogI("GMRecTipsView", "Game icon object is invalid.");
        end;

        -- 上报数据
        TGMRecTipsView.ReportTips(nVideo, nImage);

        -- 调整按钮位置
        if (nil ~= TGMRecTipsView.pBtnCel) and (nil ~= TGMRecTipsView.pBtnEdtV) and 
           (nil ~= TGMRecTipsView.pBtnEdtP) then
            local   rtTemp  = TGMRecTipsView.pBtnCel:GetRectEx(FALSE,TRUE);
            local   nWidth  = rtTemp.right  - rtTemp.left;
            local   nHeight = rtTemp.bottom - rtTemp.top;

            if (nVideo > 0) and (nImage > 0) then  -- 图片 + 视频
                rtTemp.right  = rtTemp.left  - EGMRecTipsConst.nBtnSpace;
                rtTemp.left   = rtTemp.right - nWidth;

                TGMRecTipsView.pBtnEdtP:Show(TRUE);
                TGMRecTipsView.pBtnEdtP:MoveEx(rtTemp,TRUE);

                rtTemp.right  = rtTemp.left  - EGMRecTipsConst.nBtnSpace;
                rtTemp.left   = rtTemp.right - nWidth;
                TGMRecTipsView.pBtnEdtV:Show(TRUE);
                TGMRecTipsView.pBtnEdtV:MoveEx(rtTemp,TRUE);
            elseif (nVideo > 0) then            -- 只有视频
                rtTemp.right  = rtTemp.left  - EGMRecTipsConst.nBtnSpace;
                rtTemp.left   = rtTemp.right - nWidth;

                TGMRecTipsView.pBtnEdtV:Show(TRUE);
                TGMRecTipsView.pBtnEdtV:MoveEx(rtTemp,TRUE);

                TGMRecTipsView.pBtnEdtP:Show(FALSE);
            else                                -- 只有图片
                rtTemp.right  = rtTemp.left  - EGMRecTipsConst.nBtnSpace;
                rtTemp.left   = rtTemp.right - nWidth;

                TGMRecTipsView.pBtnEdtV:Show(FALSE);
                TGMRecTipsView.pBtnEdtP:Show(TRUE);
                TGMRecTipsView.pBtnEdtP:MoveEx(rtTemp,TRUE);
            end;
        else
            TGPLog.LogE("GMRecTipsView", "Button is nil.");
        end;

        -- 移动窗体位置
        local   pUILib = ierd_tgp.tpf_ui.GetTPFUI();

        if nil ~= pUILib then
            local tParam = ierd_tgp.tpf_ui.SysParams:new();

            pUILib:get_sys_params(tParam)

            local   rtTemp = TGMRecTipsView.pDlgView:GetRectEx(TRUE,TRUE);

            rtTemp.left   = tParam.work_area_width - (rtTemp.right - rtTemp.left);
            rtTemp.right  = tParam.work_area_width;
            rtTemp.top    = tParam.work_area_height - (rtTemp.bottom - rtTemp.top);
            rtTemp.bottom = tParam.work_area_height;   -- 阴影有27个像素

            TGMRecTipsView.pDlgView:GetRectEx(rtTemp,TRUE);
            TGMRecTipsView.pDlgView:MoveEx(rtTemp,TRUE);
        else
            TGPLog.LogE("GMRecTipsView", "Get UILib object fail.");
        end;

        -- Show dialog
        TGMRecTipsView.pDlgView:Show(TRUE);
    end;

    HideView = function()
        if (nil ~= TGMRecTipsView.pDlgView) then
            TGMRecTipsView.pDlgView:Show(FALSE);
        end;

        TGMRecTipsView.zEdtParam = nil;
    end;

    -- 关闭
    OnClkClose = function(pUILib, dwBtnID)
        TGMRecTipsView.HideView();

        -- 上报数据
        TGMRecTipsView.ReportClick(ERptCmd.eClkClose);
    end;

    -- 控件ID是否有效
    IsValidID = function(dwCtrlID)
        return (nil ~= dwCtrlID) and (0 ~= dwCtrlID);
    end;

    -- 编辑视频
    OnClkEditV = function(pUILib, dwBtnID)
        -- 隐藏界面
        TGMRecTipsView.HideView();
        
        -- 上报数据
        TGMRecTipsView.ReportClick(ERptCmd.eClkEditV);

        -- 请求显示视频编辑器
        comm_center.call_service("TGPCrossIPC_ShowGVEditor", {});
    end;

    -- 编辑图片
    OnClkEditP = function(pUILib, dwBtnID)
        -- 隐藏界面
        TGMRecTipsView.HideView();
        
        -- 上报数据
        TGMRecTipsView.ReportClick(ERptCmd.eClkEditP);

        -- 打开图片上传
        local param = {};
        param.type = "profile";
        param.sub_page = "SCREENSHOT"
        param.custom_param = "jump=upload";
        if nil ~= wegame_mainframe_model and 0 ~= wegame_mainframe_model.self_tgp_id then
            param.tgp_id = wegame_mainframe_model.self_tgp_id;
        end;
        web_utility.open_link_convert(base_utility.table_to_json(param));
    end;

    -- 窗体关闭消息
    OnDlgClose = function(pUILib, dwCtrlID, bClose)
        TGMRecTipsView.HideView();

        TGMRecTipsView.pDlgView  = nil;        -- 对话框
        TGMRecTipsView.pEdtText  = nil;        -- 文字提示
        TGMRecTipsView.pBtnEdtV  = nil;        -- 编辑视频按钮
        TGMRecTipsView.pBtnEdtP  = nil;        -- 编辑图片按钮
        TGMRecTipsView.dwImgIcon = 0;          -- 游戏ICON
    end;

    -- 上报弹出TIPS
    ReportTips = function(nVideoCnt, nPictCnt)
        local   zTemp = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local   zData = {};

        zData["video"] = nVideoCnt;
        zData["image"] = nPictCnt;

        zTemp.oc_type    = 1;
        zTemp.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kGameRecordTips;
        zTemp.str_action = "GRTips";
        zTemp.str_type   = "ShowTips";
        zTemp.str_json   = base_utility.table_to_json(zData);

        base_utility.lua_qos_ex(zTemp);
        zTemp:delete();
    end;

    -- 点击按钮上报
    ReportClick = function(nType)
        --tqos上报
        local   zTemp = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local   zData = {};

        zData["type"] = nType;

        zTemp.oc_type    = 1;
        zTemp.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kGameRecordTips;
        zTemp.str_action = "GRTips";
        zTemp.str_type   = "BtnClk";
        zTemp.str_json   = base_utility.table_to_json(zData);

        base_utility.lua_qos_ex(zTemp);
        zTemp:delete();
    end;

    TipsDlgIsVisible = function()
        if TGMRecTipsView.pDlgView == nil or TGMRecTipsView.pDlgView:IsVisible() == FALSE then
            return false;
        end;
        return true;
    end;

    pDlgView  = nil;        -- 对话框
    pBtnCel   = nil;        -- 取消按钮
    pEdtText  = nil;        -- 文字提示
    pBtnEdtV  = nil;        -- 编辑视频按钮
    pBtnEdtP  = nil;        -- 编辑图片按钮
    dwImgIcon = 0;          -- 游戏ICON

    zEdtParam = nil;        -- 编辑器参数

}